package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.domain.Dept;
import cn.wolfcode.car.base.domain.Post;
import cn.wolfcode.car.base.service.IDeptService;
import cn.wolfcode.car.base.service.IPostService;
import cn.wolfcode.car.business.domain.Customer;
import cn.wolfcode.car.business.domain.CustomerLinkmen;
import cn.wolfcode.car.business.query.LinkmenQuery;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.business.service.ILinkmenService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/customerLinkmen")
public class CustomerLinkmenController {
    //模板前缀
    private static final String prefix = "business/customerLinkmen/";

    @Autowired
    private ILinkmenService linkmenService;
    @Autowired
    private IPostService postService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private ICustomerService customerService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:linkmen:view")
    @RequestMapping("/listPage")
    public String listPage(Model model){
        List<Customer> list = customerService.list();
        model.addAttribute("list",list);
        return prefix + "list";
    }

    @RequiresPermissions("business:linkmen:add")
    @RequestMapping("/addPage")
    public String addPage(Model model){
        //来源于其他表:职位,客户,部门
        List<Post> positionList = postService.list();
        List<Dept> departmentList = deptService.list();
        List<Customer> customerList = customerService.list();
        model.addAttribute("positionList",positionList);
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("customerList",customerList);
        return prefix + "add";
    }

    @RequiresPermissions("business:customerLinkmen:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        List<Post> postList = postService.list();
        List<Dept> deptList = deptService.list();
        List<Customer> customerList = customerService.list();
        model.addAttribute("postList", postList);
        model.addAttribute("deptList", deptList);
        model.addAttribute("customerList", customerList);
        model.addAttribute("customerLinkmen", linkmenService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:linkmen:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CustomerLinkmen> query(LinkmenQuery qo){
        return linkmenService.query(qo);
    }

    //新增
    @RequiresPermissions("business:linkmen:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CustomerLinkmen linkmen){
        linkmenService.save(linkmen);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:customerLinkmen:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(CustomerLinkmen customerLinkmen){
        linkmenService.update(customerLinkmen);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:customerLinkmen:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        linkmenService.deleteBatch(ids);
        return AjaxResult.success();
    }

}
